<?php
namespace App\Models\Pay;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Facades\Request;

// 玩家 根据游戏角色  新增 流失用户统计 只与昨日对比
class Recharge extends  Model{

	//protected $table='email';
    //protected $_db_select = 'mysql';

    public $timestamps = false;

	public function paginate()
    {
    	$perPage = Request::get('per_page', 15);
        $page = Request::get('page', 1);

        $start = ($page-1)*$perPage;

        $rw = \DB::connection("mysql_bgac")->table('recharge_price')->orderBy("id", 'asc');
        $total = $rw->get()->count();
        // self::SourceCondition($rw);
        $rw->skip($start)->take($perPage);
        $data = $rw->get();
        
        $data = $data->toArray();


        //$data = json_decode($data, true);
        // var_dump($data);
        // exit;

        $list = static::hydrate($data);

        $paginator = new LengthAwarePaginator($list, $total, $perPage);

        $paginator->setPath(url()->current());

        return $paginator;
    }    

    public static function getAll()
    {
        $data = \DB::connection("mysql_bgac")->table('recharge_price')->get();

        $result = [];
        foreach ($data  as $obj) {
            $result[$obj->recharge_id] = [
                'dollar'    => $obj->dollar,
                'rmb'       => $obj->rmb,
                'name'      => $obj->name,
            ];
        }
        return $result;
    }

    public static function loadJson($json_data, $coin_type = 'dollar')
    {
        
        // $trans = self::GetRechargeAlias();
        \Log::debug(__METHOD__, [$json_data]);

        foreach ($json_data as $obj) {
            $data = [
                'recharge_id' => $obj['id'],
                $coin_type => $obj['actual_price'],
                'name'  => $obj['name'],
            ];
            
            self::saveData($data);
        }

        // \Log::debug(__METHOD__, [$data]);
    }

    public static function saveData($data, $pk = 'recharge_id')
    {
        
        $one = \DB::connection("mysql_bgac")->table('recharge_price')->where($pk, $data[$pk])->first();
        
        if ( $one->id) {
            \DB::connection("mysql_bgac")->table('recharge_price')->where($pk, $data[$pk])->update($data);
        } else {
            \DB::connection("mysql_bgac")->table('recharge_price')->insert($data);
        }
    }

    public static function GetRechargeAlias()
    {
        // $all_alias_data = file_get_contents(resource_path("/json/".env("RESOURCE_CHARGE" ,'game_recharge.cn.json') ) );

        // $data_list = json_decode($all_alias_data);
        $db_data = self::GetFromChargeData();
        $result = [];
        // foreach ($data_list as $obj) {
        //     $name = '';
        //     if (isset($db_data[$obj->id])) {
        //         $name = $db_data[$obj->id]['name'];
        //     }
        //     $result[$obj->id] = $name ?: $obj->name;
        // }
        foreach ($db_data as $recharge_id => $obj) {
            $result[$recharge_id] = $obj['name'];
        }
        // \Log::debug("[RECARGE_ALIAS]COUNT[".count($result)."]", [$data_list]);
        return $result;
    }

    public static function GetFromChargeData() {
        $list = \Cache::get("ADMIN.CHARGE.PRICE");
        if ($list) {
            return json_decode($list, true);
        }
        $db_list = \DB::connection("mysql_bgac")->table('recharge_price')->get();
        $list = [];
        foreach ($db_list as $obj) {
            $list[$obj->recharge_id] = [
                'name'  => $obj->name,
                'dollar' => $obj->dollar,
                'rmb'   => $obj->rmb,
                'deal'  => $obj->deal,
            ];
        }

        \Cache::put('ADMIN.CHARGE.PRICE', json_encode($list, JSON_UNESCAPED_UNICODE), 5);
        \Log::debug("[ADMIN.CHARGE.PRICE]");
        return $list;
    }

    // 显示美元使用
    // 实际交付使用的表
    public static function GetRechargeLyto()
    {
        $data = file_get_contents(resource_path("/json/recharge_lyto.json"));
        return json_decode($data, true);       
    }

    public static function GetRecharge()
    {
        $data = file_get_contents(resource_path("/json/recharge.json"));
        return json_decode($data, true);       
    }
}